import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/presentation/widgets/segmented_progress_bar.dart';

class AnimatedSegmentedProgressBar extends StatefulWidget {
  const AnimatedSegmentedProgressBar({
    super.key,
    required this.duration,
    required this.currentProgress,
    required this.segmentCount,
    required this.backgroundColor,
    required this.progressColor,
    required this.preferredThickness,
    required this.scaleFactor,
    this.previousProgress,
    this.segmentShadow,
  });

  final Duration duration;
  final double currentProgress;
  final int segmentCount;
  final Color backgroundColor;
  final Color progressColor;
  final double preferredThickness;
  final double scaleFactor;
  final double? previousProgress;
  final BoxShadow? segmentShadow;

  @override
  State<AnimatedSegmentedProgressBar> createState() => _AnimatedSegmentedProgressBarState();
}

class _AnimatedSegmentedProgressBarState extends State<AnimatedSegmentedProgressBar>
    with SingleTickerProviderStateMixin {
  late final AnimationController _controller;
  late final Animation<double> _animation;

  late Tween<double> _progressTween;

  final easeOutTween = CurveTween(
    curve: Curves.easeOut,
  );

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: widget.duration,
    );

    _animation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeOut,
    );

    _progressTween = Tween(
      begin: widget.currentProgress,
      end: widget.currentProgress,
    );
  }

  @override
  void didUpdateWidget(covariant AnimatedSegmentedProgressBar oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (oldWidget.duration != widget.duration) {
      _controller.duration = widget.duration;
    }

    if (oldWidget.currentProgress * oldWidget.segmentCount !=
        widget.currentProgress * widget.segmentCount) {
      final previousProgress = widget.previousProgress ?? oldWidget.currentProgress;

      _progressTween = Tween(
        begin: previousProgress,
        end: widget.currentProgress,
      );

      _controller.forward(from: 0.0);
    } else if (oldWidget.segmentCount != widget.segmentCount) {
      _progressTween = Tween(
        begin: _progressTween.begin! * oldWidget.segmentCount / widget.segmentCount,
        end: _progressTween.end! * oldWidget.segmentCount / widget.segmentCount,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, _) {
        return SegmentedProgressBar(
          progress: _progressTween.evaluate(_animation),
          segmentCount: widget.segmentCount,
          backgroundColor: widget.backgroundColor,
          progressColor: widget.progressColor,
          preferredThickness: widget.preferredThickness,
          scaleFactor: widget.scaleFactor,
          segmentShadow: widget.segmentShadow,
        );
      },
    );
  }
}
